package com.example.xxpk.service.users;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xxpk.mapper.UsersMapper;
import com.example.xxpk.pojo.Users;
import com.example.xxpk.vo.LoginVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangguihong
 * @since 2025-01-21
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    @Override
    public Users login(LoginVo loginVo) {
        LambdaQueryWrapper<Users> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Users::getOpenId,loginVo.getWechatCode());
        Users user = this.getOne(lambdaQueryWrapper);
        if(user==null){
            user=new Users();
            user.setAvatarUrl("http:"+loginVo.getWechatCode());
            user.setUsername("test:"+loginVo.getWechatCode());
            user.setOpenId(loginVo.getWechatCode());
            this.save(user);
        }
        if (user.getBan().equals("1")){
            throw  new RuntimeException("您的账号已被封禁");
        }
        return user;
    }

    @Override
    public Users userInfo(Integer userId) {
        // 使用 MyBatis-Plus 提供的 getById 方法根据 ID 查询用户
        Users user = this.getById(userId);
        if (user.getBan().equals("1")){
            throw  new RuntimeException("您的账号已被封禁");
        }
        return user;
    }

    @Override
    public List<Users> userRanking() {

        return this.baseMapper.userRanking();
    }
}
